//
//  StatusCellTopView.swift
//  sinaWeibo
//
//  Created by LSY on 15/12/22.
//  Copyright © 2015年 lsy. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class StatusCellTopView: UIView {
    
//    顶部视图的底部约束
    var bottomConstraints:Constraint?
    
//    设置模型属性
    var status:Status?{
        didSet{
        
            iconImage.sd_setImageWithURL(status?.user?.headUrl)
            namelable.text = status?.user?.name
//             会员等级图片
            mbrankImage.image = status?.user?.mbrankImage
//           带V图片
            verified_type_image.image = status?.user?.verified_type_image
          // TODO:后续完善
//            timeLable.text = status?.created_at
//            sourceLabel.text = status?.source
            
//          内容
            contentLabel.text = status?.text
//           给imageURLs数组赋值
            pictureView.imageURLs = status?.imageURLs
            
//           在更新约束之前应该先卸载底部约束
            self.bottomConstraints?.uninstall()
            
//            通过判段是否有配图来添加相应的约束
            
        if let  urls = status?.imageURLs where urls.count > 0{
            
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
            })
        }else{
            self.snp_makeConstraints(closure: { (make) -> Void in
                self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMarigin).constraint
            })
            
            }
        }
    }
    
    override init(frame:CGRect){
        
        super.init(frame:frame)
        
//        backgroundColor = UIColor(white: 0.9, alpha: 1)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    设置UI
    private func setupUI(){
        addSubview(sepView)
        addSubview(iconImage)
        addSubview(namelable)
        addSubview(mbrankImage)
        addSubview(verified_type_image)
        addSubview(timeLable)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
//        更改sepView的颜色
   sepView.backgroundColor = UIColor.randomColor()
        //   添加他们的约束
        sepView.snp_makeConstraints { (make) -> Void in
            
        make.top.left.right.equalTo(self)
            make.height.equalTo(8)
        }
        //    头像
        iconImage.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(self.snp_left).offset(StatusCellMarigin)
            make.size.equalTo(CGSize(width: StatusCellHeadImageWidth, height: StatusCellHeadImageWidth))
            
        }
        
        //    姓名
        namelable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMarigin)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMarigin)
        }
        //    会员
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(namelable.snp_top)
            make.left.equalTo(namelable.snp_right).offset(StatusCellMarigin)
        }
        
        //    大v图标
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right)
            make.centerY.equalTo(iconImage.snp_bottom)
        }
        
        //    时间
        timeLable.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMarigin)
        }
        //    微博来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLable.snp_bottom)
            make.left.equalTo(timeLable.snp_right).offset(StatusCellMarigin)
        }
        //    微博文字内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(self.snp_left).offset(StatusCellMarigin)
        }
    
        //        添加配图视图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMarigin)
            //          只添加左边的约束，右边的自动计算
            make.left.equalTo(contentLabel.snp_left)
            
//           make.size.equalTo(CGSize(width: 100, height: 100))
        }

        //   设置topView 底部约束,注意修改是参照pickerView 
        self.snp_makeConstraints { (make) -> Void in
        self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
        }
        
    }
    
    //    懒加载视图
//   设置cell的分割线
    private lazy var sepView :UIView = UIView()
  private  lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
   private  lazy var namelable :UILabel = UILabel(title: "四洋洋", color: themeColor, fontSize: 14)
   private  lazy var mbrankImage :UIImageView = UIImageView(image:UIImage(named: "common_icon_membership"))
   private  lazy var verified_type_image :UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var timeLable :UILabel = UILabel(title: "11:11", color: themeColor, fontSize: 14)
    private lazy var sourceLabel:UILabel = UILabel(title: "不叫小四了", color: themeColor, fontSize: 14)
    
    private lazy var contentLabel:UILabel = UILabel(title: "少壮不努力，老大敲代码,少壮不努力，老大敲代码,少壮不努力，老大敲代码,少壮不努力，老大敲代码少壮不努力，老大敲代码", color: UIColor.darkGrayColor(), fontSize: 14,margin:StatusCellMarigin)
  
    
//    懒加载配图视图
    private lazy var pictureView:StatusPictureView = StatusPictureView()
    
    
}
